{%- if storeConfig.bing_uet_id -%}
<!-- BING Code -->
<script>
    (function (w, d, t, r, u) {
        var f, n, i;
        w[u] = w[u] || [], f = function () {
            var o = {ti: '{{ storeConfig.bing_uet_id }}'};
            o.q = w[u], w[u] = new UET(o), w[u].push('pageLoad')
        }, n = d.createElement(t), n.src = r, n.async = 1, n.onload = n.onreadystatechange = function () {
            var s = this.readyState;
            s && s !== 'loaded' && s !== 'complete' || (f(), n.onload = n.onreadystatechange = null)
        }, i = d.getElementsByTagName(t)[0], i.parentNode.insertBefore(n, i)
    })(window, document, 'script', '//bat.bing.com/bat.js', 'uetq');
</script>
<noscript>
    <img src='//bat.bing.com/action/0?ti="{{ storeConfig.bing_uet_id }}"&Ver=2' height='0' width='0'
         style='display:none; visibility: hidden;'/>
</noscript>
<script>
    window.uetq = window.uetq || [];
    {%- if template == 'product_detail' -%}
    window.uetq.push('event', 'view_item', {
    'event_category': '{{ product.product_type | escape}}',
    'event_label': '{{ product.spu | escape}}',
    'event_value': 1,
    'ecomm_totalvalue': '{{ product.variant.price }}',
    'revenue_value': '{{ product.variant.price }}',
    'currency': '{{ base_currency.standard_code }}',
    'ecomm_prodid': ["{{ product.spu | escape}}"],
    'ecomm_pagetype': 'product',
    'items': [
        {
        'id': '{{ product.spu | escape}}',
		'category': '{{ product.product_type | escape}}',
        'quantity': 1,
        'price': '{{ product.variant.price }}',
		'variant': '{{product.variant.sku_value| replace: "<br />", ", "}}',
        }
      ]
    });
	
    {%- elsif template == 'search_detail' and search.keywords -%}
    var prodid = [];
    {%- if search.products -%}
    {%- for product in search.products -%}
    prodid.push("{{ product.spu | escape}}");
    {%- endfor -%}
    {%- endif -%}
    window.uetq.push('event', 'search', {
    'event_label': '{{ search.keywords | escape}}',
    'event_category': '{{template}}',
    'ecomm_query': '{{ search.keywords | escape}}',
    'ecomm_prodid': prodid,
    'ecomm_pagetype': 'searchresults',
    'items': [
    {%- for product in search.products -%}
    {
    'id': '{{ product.spu | escape}}',
	'category': '{{ product.product_type | escape}}',
	'variant': '{{product.variant.sku_value| replace: "<br />", ", "}}',
    'quantity': 1,
    'price': '{{ product.variant.price}}',
    },
    {%- endfor -%}
    ]
    });

    {%- elsif controller == 'index' and action == 'index' -%}
    window.uetq.push('event', 'page_view_event', {'event_category': '{{template}}','ecomm_pagetype':'home'});

    {%- elsif template == 'blog_detail' -%}
    window.uetq.push('event', 'page_view_event',{
    'event_category': '{{template}}',
    'event_label': '{{blog.title | escape}}',
    'event_value': '{{blog.id}}'
    });
	
    {%- elsif template == 'collection_detail' -%}
    var prodid = [];
    {%- if collection.products -%}
    {%- for product in collection.products -%}
    prodid.push('{{ product.spu | escape}}');
    {%- endfor -%}
    {%- endif -%}
    window.uetq.push('event', 'view_item_list', {
    'event_category': '{{template}}',
    'event_label': '{{collection.title | escape}}',
    'event_value': '{{collection.id}}',
    'ecomm_category': '{{collection.title | escape}}',
    'ecomm_prodid': prodid,
    'ecomm_pagetype': 'category',
    'items': [
    {%- if collection.products -%}
    {%- for product in collection.products -%}
    {
    'id': '{{ product.spu | escape}}',
	'category': '{{ product.product_type | escape}}',
    'quantity': 1,
    'price': '{{ product.variant.price}}',
	'variant': '{{product.variant.sku_value| replace: "<br />", ", "}}',
    },
    {%- endfor -%}
    {%- endif -%}
    ]
    });

    {%- elsif template == 'page_detail' -%}
    window.uetq.push('event', 'page_view_event',{
    'event_category': '{{template}}',
    'event_label': '{{page.title | escape}}',
    'event_value': '{{page.id}}'
    });
	
    {%- elsif template == 'order_success' -%}
        var spu = [];
        {%- for product in order.products -%}
        spu.push("{{ product.spu | escape}}");
        {%- endfor -%}
        window.uetq.push('event', 'purchase', {
        'ecomm_prodid': spu,
        'ecomm_pagetype': 'purchase',
        {%- if storeConfig.bing_enable_ga -%}
        'ecomm_totalvalue': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
        'revenue_value': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
        {%- else -%}
        'ecomm_totalvalue': '{{ order.total_price }}',
        'revenue_value': '{{ order.total_price }}',
        {%- endif -%}
        'currency': '{{ order.currency_code }}',
        'transaction_id': '{{ order.order_number }}',
        'items': [
        {%- for product in order.products -%}
            {
            'id': '{{ product.spu | escape}}',
			'category': '{{ product.product_type | escape}}',
            'quantity': '{{ product.quantity }}',
            {%- if storeConfig.bing_enable_ga -%}
            'price': "{{ product.discount_price }}",
            {%- else -%}
            'price': "{{ product.price }}",
            {%- endif -%}
			'variant': '{{product.sku_value| replace: "<br />", ", "}}',
            },
            {%- endfor -%}
            ]
        });
		
    {%- else -%}
    window.uetq.push('event', 'page_view_event',{
    'event_category': '{{template}}'
    });
    {%- endif -%}

</script>
<script>
    function callback_bing_add_to_cart(ecdata) {
        var spu = [];
        spu.push(ecdata['ec_data_spu']);
        window.uetq.push('event', 'add_to_cart', {
            'event_category': ecdata['ec_data_category'],
            'event_label': ecdata['ec_data_spu'],
            'event_value': ecdata['ec_data_quantity'],
            'ecomm_prodid': spu,
            'ecomm_pagetype': 'cart',
            'ecomm_totalvalue': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
            'revenue_value': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
            'currency': '{{ base_currency.standard_code }}',
            'items': [{
                'id': ecdata['ec_data_spu'],
				'category': ecdata['ec_data_category'],
                'quantity': ecdata['ec_data_quantity'],
				'variant': ecdata['ec_data_sku_name'].replace('<br />',', '),
                'price': ecdata['ec_data_price']
            }]
        });
    }

    function callback_bing_generate_lead() {
        window.uetq.push('event', 'generate_lead');
    }

    function callback_bing_login() {
        window.uetq.push('event', 'login');
    }

    function callback_bing_begin_checkout() {
        window.uetq.push('event', 'begin_checkout');
    }

    function callback_bing_sign_up() {
        window.uetq.push('event', 'sign_up');
    }

    function callback_bing_share_product(ecdata, platform) {
        window.uetq.push('event', 'share_product', {
            'event_category': ecdata['ec_data_category'],
            'event_label': ecdata['ec_data_spu'],
            'event_value': ecdata['ec_data_quantity'],
            'revenue_value': ecdata['ec_data_price'],
            'currency': "{{ base_currency.standard_code }}",
        });
    }

    function callback_bing_remove_from_cart(ecdata) {
        window.uetq.push('event', 'remove_from_cart', {
            'event_category': ecdata['ec_data_category'],
            'event_label': ecdata['ec_data_spu'],
            'event_value': ecdata['ec_data_quantity'],
            'revenue_value': ecdata['ec_data_price']*ecdata['ec_data_quantity'],
            'currency': "{{ base_currency.standard_code }}"
        });
    }

    function callback_bing_add_address_information() {
        window.uetq.push('event', 'checkout_progress',{'event_category': 'add_address_infomation'});
    }

    function callback_bing_add_shipping_information() {
        window.uetq.push('event', 'checkout_progress',{'event_category': 'add_shipping_information'});
    }

    function callback_bing_add_payment_information() {
        window.uetq.push('event', 'add_payment_information');
        window.uetq.push('event', 'checkout_progress',{'event_category': 'add_payment_information'});
    }
</script>
<!--End BING Code -->
{%- endif -%}